/// Creator:      EH
/// CreatDate:    2021-06-18 今天是618，你买了吗？！
/// Description:  条码设置
Class Nur.MNISV3.Service.Barcode Extends %RegisteredObject [ Not ProcedureBlock ]
{

/// Creator：      EH
/// CreatDate：    2021-06-18
/// Description:   取条码类型   
ClassMethod GetBarcodeFlag(barcode, hospID = "", locID = "", wardID = "", ByRef outputBarcode = "", ByRef barcodeMessage = "", ByRef ordFlag = "") As %String
{
    n (barcode,hospID,locID,wardID,outputBarcode,barcodeMessage,ordFlag)
    s barcodeFlag="",outputBarcode=barcode,barcodeMessage="未识别的条码",ordFlag=""
    i (barcode="") q barcodeFlag
    k configFlagArr,conversionArr
    /// 按条码设置
    i (wardID'="") s hospID=##class(Nur.MNISV3.Service.Session).GetWardHospID(wardID)
    s settings=##class(Nur.MNISV3.Service.Config).GetBarcodeSetting(hospID,locID)
    //w ##class(Nur.OPPDA.JSON).Encode(settings)
    s index=0 while (1) {
        s index=settings.Next(index)
        q:(index="")
        s setting=settings.GetAt(index)
        s code=setting.GetAt("code")
        s prefix=setting.GetAt("prefix")
        s symbol=setting.GetAt("symbol")
        s digit=setting.GetAt("digit")
        s fn=setting.GetAt("fn")
        s conversion=setting.GetAt("conversion")
        s sheetID=setting.GetAt("sheetID")
        d GetBarcodeFlagValidate
        s conversionArr(code)=conversion
        i (configFlag) s configFlagArr(code)=1
        i (match)&('unmatch) {
            i (prefix'="")&(barcode[prefix) {
                s f=$f(barcode,prefix)-$l(prefix)
                i (f>0) s outputBarcode=$e(barcode,1,f-1)_$e(barcode,f+$l(prefix),$l(barcode))
            }
            s barcodeFlag=code q
        }
    }
    /// 转换
    d GetBarcodeFlagConvert
    /// 兼容无设置
    d GetBarcodeFlagDefault
    ///
    i (barcodeFlag'="") s barcodeMessage="",ordFlag="^LAB^MED^INF^INJ^PIS^"[barcodeFlag
    e  s barcodeMessage="未识别的条码"
    
    q barcodeFlag
GetBarcodeFlagValidate
    s match=0,unmatch=0,configFlag=0
    /// 校验方法
    i (fn'="") d
    .i (fn="工号") s fn="$o(^SSU(""SSUSR"",0,""SSUSR_Initials"",$$ALPHAUP^SSUTIL4(barcode),""""))'="""""
    .e  i (fn="检验条码") s fn="$o(^OEORD(0,""EpisNo"",barcode,""""))'="""""
    .e  i (fn["执行单：") s fn="..IsOnNurseOrder(barcode,"""_$p(fn,"执行单：",2)_""",hospID,""Y"")"
    .e  i (fn="住院号") s fn="..MrNO2RegNO(barcode,hospID)'="""""
    .e  i (fn="无登记号") s fn="..IsNoRegNOHospital(barcode,hospID)"
    .s configFlag=1
    .s x="s ret="_fn
    .try { x x } catch { s ret=0 }
    .i (ret) s match=1
    .e  s unmatch=1
    i (unmatch) q
    /// 关联单据
    i (sheetID'="") d
    .s configFlag=1
    .s ret=..IsOnNurseOrder(barcode,sheetID,hospID,"Y",.barcodeMessage)
    .i (ret) s match=1
    .e  s unmatch=1
    i (unmatch) q
    /// 文本长度
    i (digit'="") d
    .s configFlag=1
    .s text=barcode
    .i (prefix'="") s text=$replace(text,prefix,"")
    .s ret=$l(text)=digit
    .i (ret) s match=1
    .e  s unmatch=1
    i (unmatch) q
    /// 条码标志
    i (symbol'="") d
    .s configFlag=1
    .s ret=barcode[symbol
    .i (ret) s match=1
    .e  s unmatch=1
    i (unmatch) q
    /// 条码前缀
    i (prefix'="") d
    .s configFlag=1
    .s ret=barcode[prefix
    .i (ret) s match=1
    .e  s unmatch=1
    i (unmatch) q
    ///
    q
GetBarcodeFlagDefault
    i ('$g(configFlagArr("PAT")))&(barcodeFlag="") d
    .i (barcode["REG") s barcodeFlag="PAT",outputBarcode=$replace(barcode,"REG","")
    i ('$g(configFlagArr("BED")))&(barcodeFlag="") d
    .i (barcode["BED") s barcodeFlag="PAT",outputBarcode=$replace(barcode,"BED","")
    i ('$g(configFlagArr("MED")))&(barcodeFlag="") d
    .i (barcode["MED") s barcodeFlag="MED",outputBarcode=$replace(barcode,"MED","")
    .e  i (barcode["!")&(barcode["||") s barcodeFlag="MED",outputBarcode=$replace(barcode,"!","")
    i ('$g(configFlagArr("INF")))&(barcodeFlag="") d
    .i (barcode["-") s barcodeFlag="INF",outputBarcode=$replace(barcode,"-","||")
    i ('$g(configFlagArr("SSU")))&(barcodeFlag="") d
    .s conversion=$o(^SSU("SSUSR",0,"SSUSR_Initials",$$ALPHAUP^SSUTIL4(barcode),""))
    .i (conversion) s barcodeFlag="SSU",outputBarcode=conversion
    i ('$g(configFlagArr("PAT")))&(barcodeFlag="") d
    .i ($o(^PAPERi("PAPMI_PatNo",$$ALPHAUP^SSUTIL4(barcode),""))) s barcodeFlag="PAT"
    i ('$g(configFlagArr("LAB")))&(barcodeFlag="") d
    .i ($o(^OEORD(0,"EpisNo",barcode,""))) s barcodeFlag="LAB"
    i ('$g(configFlagArr("PIS")))&(barcodeFlag="") d
    .s oeoreId=$replace($p(barcode,"^"),"-","||")
    .i ($l(oeoreId,"||")=2) s oeoreId=oeoreId_"||1"
    .i ($o(^DHCAPREPTA(0,"IndexOrdExec",oeoreId,""))'="") s barcodeFlag="PIS",outputBarcode=oeoreId
    q
GetBarcodeFlagConvert
    i (barcodeFlag'="") d
    .s conversion=$replace($g(conversionArr(barcodeFlag))," ","")
    .i (conversion="工号") s conversion="$o(^SSU(""SSUSR"",0,""SSUSR_Initials"",$$ALPHAUP^SSUTIL4(barcode),""""))"
    .e  i (conversion="检验条码") s conversion="..LabNO2OrdID(barcode)"
    .e  i (conversion="住院号") s conversion="..MrNO2RegNO(barcode,hospID)"
    .q:(conversion="")
    .s x="s outputBarcode="_conversion
    .try { x x } catch { }
    q
}

/// Creator：      EH
/// CreatDate：    2021-03-30
/// Description:   医嘱是否属于单据
/// w ##class(Nur.MNISV3.Service.Barcode).IsOnNurseOrder("59||1107||1","CQSYD")   
ClassMethod IsOnNurseOrder(barcode = "", sheetCodeStr = "", hospID = "", ignoreStatus = "", ByRef barcodeMessage = "") As %String
{
    n (barcode,sheetCodeStr,hospID,ignoreStatus,barcodeMessage)
    s ^EH("IsOnNurseOrder")=$lb(barcode,sheetCodeStr,hospID,ignoreStatus,barcodeMessage)
    s ret=0,barcodeMessage=""
    s oeoreId=$replace(barcode,"^",",")
    s oeoreId=$p(oeoreId,",",1)
    s oeoreId=$replace(oeoreId,"!","")
    s oeoreId=$replace(oeoreId,"-","||")
    s P=$o(^OEC("OSTAT",0,"Code","P",""))
    s D=$o(^OEC("OSTAT",0,"Code","D",""))
    s U=$o(^OEC("OSTAT",0,"Code","U",""))
    i (oeoreId'="")&(oeoreId'["||") d
    .s labNO=oeoreId,labOrdID="",labOrdID1=""
    .s linkOew=0 f  s linkOew=$o(^OEORD(0,"EpisNo",labNO,linkOew))  q:(linkOew="")!(labOrdID'="")  d
    ..s linkChl=0 f  s linkChl=$o(^OEORD(0,"EpisNo",labNO,linkOew,linkChl))  q:(linkChl="")  d
    ...s ordStatID=$p($g(^OEORD(linkOew,"I",linkChl,1)),"^",13)
    ...i (labOrdID1="") s labOrdID1=linkOew_"||"_linkChl
    ...q:(ordStatID=P)!(ordStatID=D)!(ordStatID=U)
    ...s labOrdID=linkOew_"||"_linkChl
    .i (labOrdID="") s labOrdID=labOrdID1
    .i (labOrdID'="") s oeoreId=labOrdID
    s oew=+oeoreId,chl=$p(oeoreId,"||",2),sub=$p(oeoreId,"||",3)
    i (chl="") q ret
    i (hospID="") s hospID=##class(Nur.MNISV3.Service.Session).GetOrdHospID(oeoreId)
    s ifVer8d5=$$IfVer8d5(hospID)
    s hospID=##class(Nur.MNISV3.Service.Session).GetMainHospID(hospID)
    /// 只有status会变化，其余是定值
    i (ignoreStatus="Y") s ignoreStatus=1
    e  i (ignoreStatus="N") s ignoreStatus=0
    ///
    s sheetCodeStr=$replace(sheetCodeStr,",","^")
    s sheetCodeLength=$l(sheetCodeStr,"^")
    s j=0
    f sheetCodeIndex=1:1:sheetCodeLength {
        /// 单据设置
        s sheetCode=$p(sheetCodeStr,"^",sheetCodeIndex)
        i (+sheetCode=sheetCode) {
            s sheetID=sheetCode
        } else {
            i (ifVer8d5) s sheetID=$o(^CF.NUR.NIS.NurseSheetSetI("Code",$zcvt(hospID,"U")," "_$zcvt(sheetCode,"U"),""))
            e  s sheetID=$o(^Nur.NurseSheetSetI("CODE"," "_$zcvt(hospID,"U")," "_$zcvt(sheetCode,"U"),""))
        }
        i (ifVer8d5) {
            s rec=##class(CF.NUR.NIS.NurseSheetSet).%OpenId(sheetID)
            i ('$isobject(rec)) continue
            s priorityStr="^"_rec.NSSOecpr_"^"
            s catgoryStr="^"_rec.NSSOrdCat_"^"
            s statStr="^"_rec.NSSOrdStat_"^"
            s phcinStr="^"_rec.NSSOrdPhcin_"^"
            s specCodeStr="^"_rec.NSSSpecCode_"^"
            s recLocStr="^"_rec.NSSRecloc_"^"
            s stageStr="^"_rec.NSSOrdStage_"^"
            d rec.%Close()
        } else {
            s rec=##class(Nur.NurseSheetSet).%OpenId(sheetID)
            i ('$isobject(rec)) continue
            s priorityStr="^"_rec.oecpr_"^"
            s catgoryStr="^"_rec.ordCat_"^"
            s statStr="^"_rec.ordStat_"^"
            s phcinStr="^"_rec.ordPhcin_"^"
            s specCodeStr="^"_rec.specCode_"^"
            s recLocStr="^"_rec.recloc_"^"
            s stageStr="^"_rec.ordStage_"^"
            d rec.%Close()
        }
        /// 取值
        i ($i(j)=1) {
            i (sub'="") d
            .s ordStatID=$p($g(^OEORD(oew,"I",chl,"X",sub,"BILL")),"^",1)
            e  d
            .s ordStatID=$p($g(^OEORD(oew,"I",chl,1)),"^",13)
            s oecprID=$p($g(^OEORD(oew,"I",chl,1)),"^",8)
            s phcinID=$p($g(^OEORD(oew,"I",chl,2)),"^",7)
            s arcimID=$p($g(^OEORD(oew,"I",chl,1)),"^",2)
            s arcicID=$p($g(^ARCIM(+arcimID,+$p(arcimID,"||",2),1)),"^",10)
            s recLocID=$p($g(^OEORD(oew,"I",chl,3)),"^",6)
            i (ordStatID=P) s barcodeMessage="医嘱已停止"
            e  i (ordStatID=D) s barcodeMessage="医嘱已停止"
            e  i (ordStatID=U) s barcodeMessage="医嘱已作废"
        }
        /// 过滤
        i (ignoreStatus=1) {
            i (ordStatID=P)&(statStr'[P) continue
            e  i (ordStatID=D)&(statStr'[D) continue
            e  i (ordStatID=U)&(statStr'[U) continue
        } else {
            i (statStr'[("^"_ordStatID_"^"))&(ordStatID'="") continue
        }
        i (priorityStr'[("^"_oecprID_"^"))&(oecprID'="") continue
        i (phcinStr'[("^"_phcinID_"^"))&(phcinID'="") continue
        i (catgoryStr'[("^"_arcicID_"^"))&(arcicID'="") continue
        i (recLocStr'[("^"_recLocID_"^"))&(recLocID'="")&(recLocStr'="^^") continue
        s ret=1 q
    }
    q ret
IfVer8d5(hospitalId)
    n (hospitalId)
    q ##class(CF.NUR.MNIS.Version).Over(hospitalId,"","NurseOrder",8.5)
}

/// Creator：      EH
/// CreatDate：    2021-06-15
/// Description: 标本号转医嘱id
/// w ##class(Nur.MNISV3.Service.Barcode).LabNO2OrdID("2000001114")       
ClassMethod LabNO2OrdID(labNO = "") As %String
{
    n (labNO)
    s ordIDStr=""
    i (labNO'="") d
    ./// 检验
    .s linkOew=0 f  s linkOew=$o(^OEORD(0,"EpisNo",labNO,linkOew))  q:(linkOew="")  d
    ..s linkChl=0 f  s linkChl=$o(^OEORD(0,"EpisNo",labNO,linkOew,linkChl))  q:(linkChl="")  d
    ...s linkOrder=linkOew_"||"_linkChl
    ...s sub=$o(^OEORD(linkOew,"I",linkChl,"X",0))
    ...i (sub'="") s linkOrder=linkOew_"||"_linkChl_"||"_sub
    ...i (ordIDStr'="") s ordIDStr=ordIDStr_","
    ...s ordIDStr=ordIDStr_linkOrder
    q ordIDStr
}

/// Creator：      EH
/// CreatDate：    2021-06-18
/// Description:   住院号转登记号
/// w ##class(Nur.MNISV3.Service.Barcode).MrNO2RegNO("000252",2)
ClassMethod MrNO2RegNO(barcode = "", hospID = "") As %String
{
    n (barcode,hospID)
    s mrNO=barcode,regNO=""
    i (barcode="") q regNO
    s papmiID=##class(DHCWMR.IO.OutService).IGetPatientIDByMrNo(mrNO,"I",hospID,.err)
    i (papmiID'="") d
    .s regNO=$p($g(^PAPER(papmiID,"PAT",1)),"^",1)
    q regNO
}

/// Creator：      EH
/// CreatDate：    2022-04-07
/// Description:   湖南株洲：这个项目没有登记号的概念       
ClassMethod IsNoRegNOHospital(barcode = "", hospID = "") As %String
{
    n (barcode,hospID)
    i (barcode["C")&&(..GetAdmByBarcode(barcode,hospID)'="") q 1
    q 0
}

/// Creator：      EH
/// CreatDate：    2022-04-07
/// Description:   湖南株洲：这个项目没有登记号的概念
/// Other:         w ##class(Nur.MNISV3.Service.Barcode).GetAdmByBarcode("000686981C1")
ClassMethod GetAdmByBarcode(barcode = "", hospID = "") As %String
{
    n (barcode,hospID)
    &sql(select PAADM_RowID into:a from SQLUser.PA_ADM where PAADM_ADMNo=:barcode)
    q $g(a)
}

/// Creator：      EH
/// CreatDate：    2022-04-07
/// Description:   湖南株洲：这个项目没有登记号的概念 
/// Other:         w ##class(Nur.MNISV3.Service.Barcode).GetAllPreviousAdmByBarcode("000686981C1")
ClassMethod GetAllPreviousAdmByBarcode(barcode = "", hospID = "") As %String
{
    n (barcode,hospID)
    s l=""
    s b=$p(barcode,"C",1)
    f i=1:1:1000 d
    .s q=b_"C"_i
    .s a=""
    .&sql(select PAADM_RowID into:a from SQLUser.PA_ADM where PAADM_ADMNo=:q)
    .i ($g(a)="") s i=1000 q
    .i (l'="") s l=l_"^"
    .s l=l_a
    q l
}

}
